//
//  WMEArticleInformationTableViewCell.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/3/5.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEArticleInformationTableViewCell.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>

@interface WMEArticleInformationTableViewCell ()
@property (nonatomic, retain)UIImageView *thumbnail;/**<缩略图*/
@property (nonatomic, retain)UILabel *title;/**<标题*/
@property (nonatomic, retain)UILabel *name;/**<名字*/
@property (nonatomic, retain)UILabel *categoryGroupName;/**<专栏名*/
@property (nonatomic, retain)UILabel *read;/**<*/
@end

@implementation WMEArticleInformationTableViewCell

- (void)dealloc
{
    [_thumbnail release];
    [_title release];
    [_name release];
    [_categoryGroupName release];
    [_read release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.thumbnail = [[UIImageView alloc] init];
//        _thumbnail.backgroundColor = [UIColor redColor];
//        _thumbnail.contentMode = UIViewContentModeScaleAspectFill;
//        _thumbnail.layer.contentsRect = CGRectMake(0, 0, 1, 0.6);
//        _thumbnail.clipsToBounds = YES;
        
//        [_thumbnail sizeToFit];
        [self.contentView addSubview:_thumbnail];
        [_thumbnail release];
        
        self.title = [[UILabel alloc] init];
//        _title.backgroundColor = [UIColor orangeColor];
        [self.contentView addSubview:_title];
        [_title release];
        
        self.name = [[UILabel alloc] init];
//        _name.backgroundColor = [UIColor yellowColor];
        _name.textColor = [UIColor lightGrayColor];
        _name.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:_name];
        [_name release];
        
        self.categoryGroupName = [[UILabel alloc] init];
//        _categoryGroupName.backgroundColor = [UIColor blueColor];
        _categoryGroupName.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:_categoryGroupName];
        [_categoryGroupName release];
        
        self.read = [[UILabel alloc] init];
//        _read.backgroundColor = [UIColor purpleColor];
        _read.font = [UIFont systemFontOfSize:12];
        _read.textColor = [UIColor lightGrayColor];
        _read.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:_read];
        [_read release];
    }
    return self;
}

- (void)setModel:(WMEArticleInformationModel *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
        NSLog(@"%@", _model.articleThumbnail);
        [_thumbnail sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@-list.jpeg", _model.articleThumbnail]]];
        _title.text = _model.articleTitle;
        _name.text = _model.authorName;
        _categoryGroupName.text = _model.categoryGroupName;
        _read.text = _model.articleStatsRead.stringValue;
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    __block __typeof(&*self)blockself = self;
    if (WIDTH > 420) {
        NSLog(@"横屏了");
        //横屏时width的 长度变化了 需要更新
        [_thumbnail mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(blockself.mas_left).with.offset(10);
            make.top.equalTo(blockself.mas_top).with.offset(10);
            make.bottom.equalTo(blockself.mas_bottom).with.offset(-10);
            make.size.mas_equalTo(CGSizeMake(150 *WIDTH/736, 100 *WIDTH / 736));
        }];
        
        [_title mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_thumbnail.mas_right).with.offset(10);
            make.right.equalTo(blockself.mas_right).with.offset(-10);
            make.top.equalTo(blockself.mas_top).with.offset(10);
            make.height.equalTo(@25);
        }];
        
        [_categoryGroupName mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_title.mas_bottom).with.offset(10);
            make.left.equalTo(_thumbnail.mas_right).with.offset(10);
            make.width.equalTo(@25);
            make.height.equalTo(@25);
        }];

        _name.textAlignment = NSTextAlignmentCenter;
        [_name mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_categoryGroupName.mas_right).with.offset(10);
            make.top.equalTo(_title.mas_bottom).with.offset(10);
            make.height.equalTo(@25);
            make.width.equalTo(@240);
        }];
        
        [_read mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(blockself.mas_right).with.offset(-10);
            make.top.equalTo(_title.mas_bottom).with.offset(10);
            make.left.equalTo(_name.mas_right).with.offset(10);
            make.height.equalTo(@25);
        }];

    }else{
//        _thumbnail.contentMode = UIViewContentModeTop;
//        _thumbnail.clipsToBounds = YES;
        [_thumbnail mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo (blockself.mas_left).with.offset(10);
            make.top.equalTo(blockself.mas_top).with.offset(10);
            make.bottom.equalTo(blockself.mas_bottom).with.offset(-10);
            make.size.mas_equalTo(CGSizeMake(150*WIDTH/414, 100 * WIDTH /414));
        }];
        
        [_title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_thumbnail.mas_right).with.offset(10);
            make.right.equalTo(blockself.mas_right).with.offset(-10);
            make.top.equalTo(blockself.mas_top).with.offset(10);
            make.height.equalTo(@25);
        }];
        
        [_categoryGroupName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_title.mas_bottom).with.offset(10);
            make.left.equalTo(_thumbnail.mas_right).with.offset(10);
            make.width.equalTo(@25);
            make.height.equalTo(@25);
        }];
        
        _name.textAlignment = NSTextAlignmentLeft;
        [_name mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_categoryGroupName.mas_right).with.offset(10);
            make.top.equalTo(_title.mas_bottom).with.offset(10);
            make.height.equalTo(@25);
            make.width.equalTo(@120);
        }];
        
        [_read mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(blockself.mas_right).with.offset(-10);
            make.top.equalTo(_title.mas_bottom).with.offset(10);
            make.left.equalTo(_name.mas_right).with.offset(10);
            make.height.equalTo(@25);
        }];
    }
}

//计算文本高
+ (CGFloat)heightWithText:(NSString *)text FontOfSize:(CGFloat )fontOfSize MySize:(CGSize)size
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontOfSize]};
    //计算 文本 需的rectangular
    CGRect r = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //返回文本 的 高
    return r.size.height;
}


//计算文本宽
+ (CGFloat)widthWithText:(NSString *)text MySize:(CGSize)size MyFontSize:(CGFloat)fontSize
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    //    CGSize size = CGSizeMake(WIDTH - 100, 10);
    CGRect r = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return r.size.width;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
